package com.elite.groovy.callback


import com.alibaba.fastjson.JSONObject
import com.elite.groovy.kh.KHUtil
import com.elite.groovy.util.EliteBean
import com.wisdge.dataservice.Result
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @Title: 项目表数据同步接口
 *
 */

public class apiAgentStateCallback implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiAgentStateCallback.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        String getMethod = request.getMethod();
        log.debug("apiAgentStateCallback getMethod:" + getMethod);

        // 获取所有Header
        Map<String, String> mapHeader = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            log.debug("Call Header-" + key + ":" + value);

            mapHeader.put(key, value);
        }

        // 获取所有参数名
        Map<String, String> mapParam = new HashMap<String, String>();
        Enumeration ep = request.getParameterNames();
        while (ep.hasMoreElements()) {
            String key = (String) ep.nextElement();
            String value = request.getParameter(key);
            log.debug("Call Parameter-" + key + ":" + value);
            mapParam.put(key, value);

        }

        // 获取调用数据
        String Syncpram = "";
        if (getMethod.equals("GET")) {
            Syncpram = request.getQueryString();
            log.info("接收调用GET参数信息：" + Syncpram);
        } else if (getMethod.equals("POST")) {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            BufferedReader iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                Syncpram += line;
            }
            log.info("接收调用POST报文信息：" + System.getProperty("line.separator") + Syncpram);
        }


        try {
            JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

            // 获取任务配置基础数据
            net.sf.json.JSONObject stateJson = net.sf.json.JSONObject.fromObject(Syncpram);

            String enterpriseId = KHUtil.GetSFJV(stateJson, "enterpriseId");
            String callType = KHUtil.GetSFJV(stateJson, "callType");
            String cno = KHUtil.GetSFJV(stateJson, "cno");
            String loginStatus = KHUtil.GetSFJV(stateJson, "loginStatus");
            String deviceStatus = KHUtil.GetSFJV(stateJson, "deviceStatus");
            String pauseDescription = KHUtil.GetSFJV(stateJson, "pauseDescription");
            String wrapupTime = KHUtil.GetSFJV(stateJson, "wrapupTime");
            String mainUniqueId = KHUtil.GetSFJV(stateJson, "mainUniqueId");
            String code = KHUtil.GetSFJV(stateJson, "code");
            String action = KHUtil.GetSFJV(stateJson, "action");
            String pauseOpType = KHUtil.GetSFJV(stateJson, "pauseOpType");
            String bindTel = KHUtil.GetSFJV(stateJson, "bindTel");
            String startTime = KHUtil.GetSFJV(stateJson, "startTime");
            String startTimeMillis = KHUtil.GetSFJV(stateJson, "startTimeMillis");
            String lastStartTime = KHUtil.GetSFJV(stateJson, "lastStartTime");
            String lastStartTimeMillis = KHUtil.GetSFJV(stateJson, "lastStartTimeMillis");
            log.debug("查询当前坐席状态数据【" + cno + "】：" + loginStatus);

            String insertSQL = '''Insert Into Trc2_Agent_State_His
                                      (Rec_Guid,
                                       Enterpriseid,
                                       Calltype,
                                       Cno,
                                       Loginstatus,
                                       Devicestatus,
                                       Pausedescription,
                                       Wrapuptime,
                                       Mainuniqueid,
                                       Code,
                                       Action,
                                       Pauseoptype,
                                       Bindtel,
                                       Starttime,
                                       Starttimemillis,
                                       Laststarttime,
                                       Laststarttimemillis,
                                       Updatetime)
                                    Values
                                      (Uuid(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, Now())''';
            int num = jt.update(insertSQL, enterpriseId, callType, cno, loginStatus, deviceStatus, pauseDescription, wrapupTime, mainUniqueId, code, action, pauseOpType, bindTel, startTime, startTimeMillis, lastStartTime, lastStartTimeMillis);
            fff.debug("查询当前坐席状态数据【" + cno + "】 DB结果：" + num);


        } catch (Exception e) {
            log.error("apiAgentStateCallback HTTP请求调用错误，接口处理异常：" + e.getMessage(), e);
            return JSONObject.toJSONString(new Result(Result.ERROR, "apiAgentStateCallback HTTP请求调用错误，接口处理异常"));

        }

        return JSONObject.toJSONString(new Result(Result.SUCCESS, "apiAgentStateCallback数据同步完成"));
    }

}